package com.xaslsoft.xaslsoft.device.utils;

import java.nio.ByteBuffer;
import java.util.Locale;

/**
 * Byte转换工具
 *
 * @author yangle
 */
public class ByteUtils {

	static char chs[] = new char[36];

	static {
		for (int i = 0; i < 10; i++) {
			chs[i] = (char) ('0' + i);
		}
		for (int i = 10; i < chs.length; i++) {
			chs[i] = (char) ('A' + (i - 10));
		}
	}

	/**
	 * 十六进制字符串转byte[]
	 *
	 * @param hex 十六进制字符串
	 * @return byte[]
	 */
	public static byte[] hexStr2Byte(String hex) {
		if (hex == null) {
			return new byte[]{};
		}

		// 奇数位补0
		if (hex.length() % 2 != 0) {
			hex = "0" + hex;
		}

		int length = hex.length();
		ByteBuffer buffer = ByteBuffer.allocate(length / 2);
		for (int i = 0; i < length; i++) {
			String hexStr = hex.charAt(i) + "";
			i++;
			hexStr += hex.charAt(i);
			byte b = (byte) Integer.parseInt(hexStr, 16);
			buffer.put(b);
		}
		return buffer.array();
	}

	/**
	 * byte[]转十六进制字符串
	 *
	 * @param array byte[]
	 * @return 十六进制字符串
	 */
	public static String byteArrayToHexString(byte[] array) {
		if (array == null) {
			return "";
		}
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			buffer.append(byteToHex(array[i]));
		}
		return buffer.toString();
	}

	/**
	 * byte转十六进制字符
	 *
	 * @param b byte
	 * @return 十六进制字符
	 */
	public static String byteToHex(byte b) {
		String hex = Integer.toHexString(b & 0xFF);
		if (hex.length() == 1) {
			hex = '0' + hex;
		}
		return hex.toUpperCase(Locale.getDefault());
	}


	/**
	 * 将二进制转换成16进制
	 *
	 * @param buf
	 * @return
	 */
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制
	 *
	 * @param hexStr
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr == null || hexStr.length() == 0) {
			return null;
		}
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0, foot = 0; i < hexStr.length(); i += 2, foot++) {
			String temp = hexStr.substring(i, i + 2);//切割 16进制 二位为一个单位转换
			result[foot] = (byte) Integer.parseInt(temp, 16);
		}
		return result;
	}

	public static byte[] hexStr2Byte2(String hex) {
		ByteBuffer bf = ByteBuffer.allocate(hex.length() / 2);
		for (int i = 0; i < hex.length(); i++) {
			String hexStr = hex.charAt(i) + "";
			i++;
			hexStr += hex.charAt(i);
			byte b = (byte) Integer.parseInt(hexStr, 16);
			bf.put(b);
		}
		return bf.array();
	}

	public static String[] Convert16To2(String str16) {
		String[] str = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
			"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
		String[] str2 = new String[str16.length()];
		StringBuffer sb = new StringBuffer();
		int j = 0;
		for (int i = 0; i < str16.length(); i++) {
			char c = str16.charAt(i);
			int x;
			if (c < '0' || c > '9') {
				x = c - 'A' + 10;
			} else {
				x = c - '0';
			}
			str2[j] = str[x];
			j++;
		}
		return str2;
	}

	/**
	 * 转换方法
	 *
	 * @param num       元数据字符串
	 * @param fromRadix 元数据的进制类型
	 * @param toRadix   目标进制类型
	 * @return
	 */
	public static String transRadix(String num, int fromRadix, int toRadix) {
		int number = Integer.valueOf(num, fromRadix);
		StringBuilder sb = new StringBuilder();
		while (number != 0) {
			sb.append(chs[number % toRadix]);
			number = number / toRadix;
		}
		if (number == 0) {
			sb.append("0000");
		}
		String data = String.format("%04d", Integer.parseInt(sb.reverse().toString()));
		return data;

	}
}
